package midone.exchangeskill.base.view;

import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import midone.exchangeskill.utils.RxBus;
import midone.exchangeskill.utils.RxUtils;
import midone.exchangeskill.utils.StringUtils;
import midone.exchangeskill.utils.ViewUtils;
import rx.Subscription;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;


/**
 * Created by NekoNeko on 2015/11/23.
 */
public class BaseFragment extends Fragment {
    int screenHeight;
    int screenWidth;
    private CompositeSubscription mCompositeSubscription;

    public int getScreenWidth() {
        return screenWidth;
    }

    public int getScreenHeight() {
        return screenHeight;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (this.mCompositeSubscription != null) {
            this.mCompositeSubscription.unsubscribe();//取消注册，以避免内存泄露
            this.mCompositeSubscription.clear();
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rxBusObservers();
    }

    /**
     * 订阅消息
     */
    private void rxBusObservers() {
        Subscription subscription = RxBus.getInstance()
                .toObserverable()
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object event) {
                        if (event instanceof RxBus.RxMessageEvent) {
                            //do something
                            onRxBusMessageEvent((RxBus.RxMessageEvent) event);
                        }
                    }
                });
        addSubscription(subscription);
    }

    /**
     * rx订阅消息
     *
     * @param rxMessageEvent
     */
    protected void onRxBusMessageEvent(RxBus.RxMessageEvent rxMessageEvent) {
    }

    /**
     * 把订阅添加到CompositeSubscription
     *
     * @param subscription
     */
    public void addSubscription(Subscription subscription) {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = new CompositeSubscription();
        }

        this.mCompositeSubscription.add(subscription);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    public void showToastMessage(String msg) {
        if (msg == null) {
            return;
        }
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }


    public void showToastMessage(final String msg, final View view) {
        if (msg == null) {
            return;
        }
        Subscription subscription = RxUtils.newRx().debouce(new RxUtils.DelayCall() {
            @Override
            public void onCall() {
                Toast toast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setView(view);
                toast.show();
            }
        });
        addSubscription(subscription);
    }

    public void showSnackBar(String msg) {
        if (getView() == null || getActivity() == null || isEmpty(msg))
            return;
        Snackbar.make(getView(), msg, Snackbar.LENGTH_SHORT).show();
    }

    public void onSnackBarAction(View actionView) {

    }

    /**
     * 带action 按钮的snackBar
     *
     * @param msg
     * @param action
     * @param onClickListener
     */
    public void showSnackBar(String msg, String action, View.OnClickListener onClickListener) {
        if (isEmpty(msg) || getView() == null || getActivity() == null)
            return;
        Snackbar snackbar = Snackbar.make(getView(), msg, Snackbar.LENGTH_INDEFINITE).setAction(action, onClickListener);
        snackbar.show();
    }

    public void showSnackBar(String msg, String actionText) {
        if (isEmpty(msg) || getView() == null || getActivity() == null)
            return;
        Snackbar.make(getView(), msg, Snackbar.LENGTH_SHORT).setAction(actionText, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSnackBarAction(v);
            }
        }).show();
    }

    /**
     * 是否为空
     *
     * @param str
     * @return
     */
    public boolean isEmpty(String str) {
        return StringUtils.isEmpty(str);
    }


    @Override
    public void onResume() {
        super.onResume();
        if (!isHidden())
            onShow();
    }

    @Override
    public void onPause() {
        super.onPause();
        onHide();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!isHidden()) {
            onShow();
        } else {
            onHide();
        }
    }

    /**
     * 当前fragment可见
     */
    @CallSuper
    public void onShow() {

    }

    /**
     * fragment不可见
     */
    @CallSuper
    public void onHide() {

    }

}
